package com.demo.easy;

import java.time.LocalDateTime;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.StringUtils;
import com.demo.util.DateContants;

public class LocalDateTimeonverter implements Converter<LocalDateTime> {

    @Override
    public Class<?> supportJavaTypeKey() {
        return LocalDateTime.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    /**
     * 这里读的时候会调用
     *
     * @param context
     * @return
     */
    @Override
    public LocalDateTime convertToJavaData(ReadConverterContext<?> context) {
        String text = context.getReadCellData().getStringValue();
        if (!StringUtils.isBlank(text)) {
            try {
                return LocalDateTime.parse(text,DateContants.FULL_TIME_FORMATTER);
            } catch (Exception e) {
            }
        }
        return null;
    }

    @Override
    public WriteCellData<?> convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty,
        GlobalConfiguration globalConfiguration) throws Exception {
        if (value != null) {

            return new WriteCellData<String>(value.format(DateContants.FULL_TIME_FORMATTER));
        }
        return null;
    }

}
